package cl.pelu.operacion;

import java.awt.Frame;
import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.table.DefaultTableModel;

import cl.pelu.main.PeluqueriaApp;
import cl.pelu.model.bo.dvo.BoletaBO;
import cl.pelu.model.bo.dvo.BoletaDVO;
import cl.pelu.model.bo.dvo.DetalleDVO;
import cl.pelu.model.bo.dvo.FormulaBO;
import cl.pelu.utils.PeluFacade;


public class BuscarBoletaForm  extends javax.swing.JInternalFrame {

	private static BuscarBoletaForm myInstance;
	
	private javax.swing.JLayeredPane busquedaBoletaLayerPane;
	private javax.swing.JLayeredPane opcionesBoletaLayerPane;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable boletaTable;
	private javax.swing.JLabel fechaDesdeLabel;
	private javax.swing.JLabel fechaHastaLabel;
	private javax.swing.JLabel rutClienteLabel;
	private javax.swing.JLabel nombreClienteLabel;
	private javax.swing.JTextField fechaHastaField;
	private javax.swing.JTextField fechaDesdeField;
	private javax.swing.JTextField rutClienteField;
	private javax.swing.JTextField nombreClienteField;
	private SimpleDateFormat df ;
	private javax.swing.JButton buscarClienteButton;
	private javax.swing.JButton buscarButton;
	private javax.swing.JButton cargarButton;
	private javax.swing.JButton eliminarButton;
	private JRadioButton buscarPorCliente;
	private JRadioButton buscarPorFecha;
	
	private ButtonGroup  group ;
	
	
private Date date;	
	/** Creates new form BuscarCliente */
	public BuscarBoletaForm() {
		initComponents();
	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {
		
		buscarPorCliente = new JRadioButton("Buscar Por Cliente");
		buscarPorFecha = new JRadioButton("Buscar Por Fecha");
		group = new ButtonGroup();
		
		group.add(buscarPorCliente);
		group.add(buscarPorFecha);
		

		jScrollPane1 = new javax.swing.JScrollPane();
		boletaTable = new javax.swing.JTable();
		busquedaBoletaLayerPane = new javax.swing.JLayeredPane();
		opcionesBoletaLayerPane = new javax.swing.JLayeredPane();
		
		fechaDesdeLabel = new javax.swing.JLabel("Fecha Desde");
		fechaHastaLabel = new javax.swing.JLabel("Fecha Hasta");
		rutClienteLabel = new javax.swing.JLabel("Rut Cliente");
		nombreClienteLabel = new  javax.swing.JLabel("Nombre Cliente");
		fechaDesdeField = new javax.swing.JTextField();
		fechaHastaField = new javax.swing.JTextField();
		rutClienteField = new javax.swing.JTextField();
		nombreClienteField = new javax.swing.JTextField();
		
		buscarButton = new javax.swing.JButton("Buscar");
		cargarButton = new  javax.swing.JButton("Cargar");
		eliminarButton = new  javax.swing.JButton("Eliminar");
		
		buscarClienteButton = new  javax.swing.JButton("Buscar Cliente");
		
		cargarButton.setEnabled(false);
		eliminarButton.setEnabled(false);
		rutClienteField.setEnabled(false);
		nombreClienteField.setEnabled(false);
		
		 df = new SimpleDateFormat("dd/MM/yyyy");
		 date = new Date();
		 
		 fechaDesdeField.setText(df.format(date)); 
		 fechaHastaField.setText(df.format(date));
		
		 

		
		
		

		setClosable(true);

		boletaTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] {

				}, new String[] { "Numero Boleta", "fecha", "observacion", "subtotal",
						"total" }) {
			boolean[] canEdit = new boolean[] { false, false, false, false,		false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		
		

		jScrollPane1.setViewportView(boletaTable);

		busquedaBoletaLayerPane.setBorder(javax.swing.BorderFactory	.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(),	"Busqueda Boletas"));

		opcionesBoletaLayerPane.setBorder(javax.swing.BorderFactory	.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(),	"Opciones"));
		
		rutClienteLabel.setBounds(30, 30, 150, 20);
		nombreClienteLabel.setBounds(30, 60, 150, 20);
		
		rutClienteField.setBounds(200, 30, 150, 20);
		nombreClienteField.setBounds(200, 60, 150, 20);
		
		fechaDesdeLabel.setBounds(30, 90, 150, 20);
		fechaDesdeField.setBounds(200, 90, 150, 20);
		
		fechaHastaLabel.setBounds(400, 90, 150, 20);
		fechaHastaField.setBounds(600, 90, 150, 20);
		buscarButton.setBounds(800, 90, 150, 20);
		
		cargarButton.setBounds(600, 15, 150, 20);
		eliminarButton.setBounds(800, 15,150, 20);
		
		buscarClienteButton.setBounds(400, 15, 150, 20);

		buscarPorCliente.setBounds(30, 120, 200, 20);
		buscarPorFecha.setBounds(300, 120, 150, 20);

		
		busquedaBoletaLayerPane.add(fechaDesdeLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaBoletaLayerPane.add(fechaHastaLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaBoletaLayerPane.add(fechaDesdeField,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaBoletaLayerPane.add(fechaHastaField,javax.swing.JLayeredPane.DEFAULT_LAYER);
		
		busquedaBoletaLayerPane.add(rutClienteLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaBoletaLayerPane.add(nombreClienteLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaBoletaLayerPane.add(rutClienteField,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaBoletaLayerPane.add(nombreClienteField,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaBoletaLayerPane.add(buscarClienteButton,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaBoletaLayerPane.add(buscarPorCliente,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaBoletaLayerPane.add(buscarPorFecha,javax.swing.JLayeredPane.DEFAULT_LAYER);
		
		
		
		busquedaBoletaLayerPane.add(buscarButton,javax.swing.JLayeredPane.DEFAULT_LAYER);
		opcionesBoletaLayerPane.add(cargarButton,javax.swing.JLayeredPane.DEFAULT_LAYER);
		opcionesBoletaLayerPane.add(eliminarButton,javax.swing.JLayeredPane.DEFAULT_LAYER);

		buscarPorFecha.setSelected(true);
		
		
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(49, 49, 49)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
											.addComponent(
														busquedaBoletaLayerPane,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														1000,
														javax.swing.GroupLayout.PREFERRED_SIZE).addGap(27, 27, 27)
								
											.addComponent(
														jScrollPane1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														1000,
														javax.swing.GroupLayout.PREFERRED_SIZE).addGap(27, 27, 27)
										    .addComponent(
														opcionesBoletaLayerPane,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														1000,
														javax.swing.GroupLayout.PREFERRED_SIZE))
																
											.addContainerGap(134, Short.MAX_VALUE)));
		
		
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addGap(17, 17, 17)
						.addComponent(busquedaBoletaLayerPane,
								javax.swing.GroupLayout.PREFERRED_SIZE, 150,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addComponent(jScrollPane1,
								javax.swing.GroupLayout.PREFERRED_SIZE, 300,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addComponent(opcionesBoletaLayerPane,
								javax.swing.GroupLayout.PREFERRED_SIZE, 50,
								javax.swing.GroupLayout.PREFERRED_SIZE)								
						.addContainerGap(99, Short.MAX_VALUE)));
		

		pack();
		
		
		
		boletaTable.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jTable1MouseClicked(evt);
			}
		});
		
		
		buscarButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					buscarButtonActionPerformed(evt);
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(null, e.getMessage(), e.toString(), JOptionPane.ERROR_MESSAGE);
				} catch (ParseException e) {
					JOptionPane.showMessageDialog(null, e.getMessage(), e.toString(), JOptionPane.ERROR_MESSAGE);
				}
			}
	});
		 cargarButton .addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					try {
						cargarButtonActionPerformed(evt);
					} catch (PropertyVetoException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
		});
		 eliminarButton .addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					try {
						eliminarButtonActionPerformed(evt);
					} catch (SQLException e) {
						JOptionPane.showMessageDialog(null, e.getMessage(), e.toString(), JOptionPane.ERROR_MESSAGE);
					} catch (ParseException e) {
						JOptionPane.showMessageDialog(null, e.getMessage(), e.toString(), JOptionPane.ERROR_MESSAGE);
					}
				}
		});
		 
		 buscarClienteButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					try {
						buscarClienteButtonActionPerformed(evt);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (PropertyVetoException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
		});		 
		 
		
	}

	
	
	private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
		
		cargarButton.setEnabled(true);
		eliminarButton.setEnabled(true);
	}


	
	public static BuscarBoletaForm getInstance() throws PropertyVetoException {
		if (myInstance == null) {
			myInstance = new BuscarBoletaForm();
		}
		return myInstance;
	}
	
	
	private void cargarTabla(BoletaDVO[] boleta) {
		
		boletaTable.removeAll();
		
		if(boleta != null){
			
			
		DefaultTableModel tableModel = (DefaultTableModel) this.boletaTable.getModel();

		for (BoletaDVO boletaDVO : boleta) {
			if(boletaDVO != null){
				tableModel.addRow(new Object[] { boletaDVO.getId()
						,df.format(boletaDVO.getFecha())
						,boletaDVO.getObservacion()
						,boletaDVO.getSubTotal()
						,boletaDVO.getTotal()
				
				});
				
			}

		}

		}
	}
		private void buscarButtonActionPerformed(java.awt.event.ActionEvent evt) throws SQLException, ParseException {
			
			if(buscarPorFecha.isSelected()){
				
				limpiarTabla();
				Date fechaDesde = df.parse(fechaDesdeField.getText());
				Date fechaHasta =  df.parse(fechaHastaField.getText());
				BoletaDVO[] boleta = PeluFacade.getPeluqueriaComponent().getBoletaByFechas(fechaDesde, fechaHasta);
				this.cargarTabla(boleta);
				cargarButton.setEnabled(false);
				eliminarButton.setEnabled(false);
				
			}else{
				
				limpiarTabla();
				BoletaDVO[] boleta = PeluFacade.getPeluqueriaComponent().getBoletaByCliente(rutClienteField.getText());
				this.cargarTabla(boleta);
				cargarButton.setEnabled(false);
				eliminarButton.setEnabled(false);
				
				
			}
			
			
			
			
			
		}
		

		public void limpiarTabla (){
			
			DefaultTableModel tableModel = (DefaultTableModel) this.boletaTable.getModel();
			
			int a = tableModel.getRowCount();
			
			for (int i = 0; i < a; i++) {
				tableModel.removeRow(0);
				
			}
			
		}
		

		
		private void eliminarButtonActionPerformed(java.awt.event.ActionEvent evt) throws SQLException, ParseException {
		
			int confirmado = JOptionPane.showConfirmDialog(null,"seguro que quires eliminar la boleta");
			if (JOptionPane.OK_OPTION == confirmado) {

				int id = Integer.parseInt(""+boletaTable.getValueAt(boletaTable.getSelectedRow(), 0));
				PeluFacade.getPeluqueriaComponent().eliminarBoleta(id);
				buscarButtonActionPerformed(null);
			
			}
			
						
		}
		
		
		private void cargarButtonActionPerformed(java.awt.event.ActionEvent evt) throws PropertyVetoException, SQLException {
			 	
				int id = Integer.parseInt(""+boletaTable.getValueAt(boletaTable.getSelectedRow(), 0));
			 	
			 	BoletaBO boleta = PeluFacade.getPeluqueriaComponent().getBoletaBObyId(id);
			 	Boleta mantenedor;
				mantenedor = Boleta.getInstance();
				mantenedor.limpiarFormulario();
				mantenedor.cargarBoleta(boleta);
				
				PeluqueriaApp.jDesktopPane1.removeAll();
				PeluqueriaApp.jDesktopPane1.add(mantenedor);
				mantenedor.setMaximum(true);
				mantenedor.setVisible(true);
			
			
		}
		
		private void 	buscarClienteButtonActionPerformed(java.awt.event.ActionEvent evt) throws PropertyVetoException, SQLException {
		 	
			Frame frame = JOptionPane.getFrameForComponent(this);
			BuscarCliente buscarCliente  = BuscarCliente.getInstance(frame,true);
			buscarCliente.limpiarFormulario();
			buscarCliente.setVisible(true);
			
			rutClienteField.setText(buscarCliente.getRut());
			nombreClienteField.setText(buscarCliente.getNombre());
			
			
			
		
		}	
	


}